<?php

class OrderAction extends AdminAction
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单列表
     */
    public function index()
    {
        //登录用户信息
        $user = Session('enterprise');
        $user_id = 1;

        //获取已经投放的广告
        $ad_putRelationship = M("putrelationship");
        $adInfo = $ad_putRelationship->where(array('advertising_user_id'=>$user_id))
            ->order('rel_id desc')
            ->field('advertising_id, advertising_name')
            ->select();

        //获取所有网站
        $site = M('site');
        $siteInfo = $site->field('site_id, site_name')->select();

        $isSelect = intval(I('isSelect'));

        //print_r($_POST);
        /*
         * Array ( [advertising_id] => 5 [site_id] => 25 [isSelect] => 1 )
         */

        if ($isSelect) {
            //广告ID
            $advertising_id = intval(I('advertising_id'));

            //网站ID
            $site_id = intval(I('site_id'));

            //分页每页的条数
            $perPage = $this->getPerPage();

            //查询条件
            $where = array();
            $where['advertising_id'] = $advertising_id;
            $where['site_id'] = $site_id;

            $currPage = intval($_GET['p']);
            $currPage < 1 && $currPage = 1;

            $ad_order = M("order");

            $itemCount = $ad_order->where($where)->count();
            $data = $ad_order->where($where)->order('order_id desc')->limit(($currPage-1)*$perPage, $perPage)->select();

            $itemPerPage = $perPage;
            $pageUrl = $this->baseUrl . '/Admin/Order/index';
            $paramQuery = array('advertising_id'=>$advertising_id, 'site_id'=>$site_id, 'isSelect'=>$isSelect);
            $paginator = new Paginator($currPage, $itemCount, $itemPerPage, $pageUrl, $paramQuery);

            $page = $paginator->getHtml(1);

        }

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('adInfo', $adInfo);
        $this->assign('siteInfo', $siteInfo);
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('advertising_id', $advertising_id);
        $this->assign('site_id', $site_id);

        $this->display();
    }

    public function detail()
    {
        //分页每页的条数
        $perPage = $this->getPerPage();

        //查询条件
        $where = array();

        //登录用户信息
        $user = Session('enterprise');
        $user_id = $user['enterprise_id'];
        $where['advertising_user_id'] = 1;

        $ad   = !empty(I('ad')) ? I('ad') : '';
        $site = !empty(I('site')) ? I('site') : '';

        if ($ad) {
            $where['advertising_name'] = $ad;
        }

        if ($site) {
            $where['site_name'] = $site;
        }

        $currPage = intval($_GET['p']);
        $currPage < 1 && $currPage = 1;

        $ad_putRelationship = M("order");

        $itemCount = $ad_putRelationship->where($where)->count();
        $data = $ad_putRelationship->where($where)->order('order_id desc')->limit(($currPage-1)*$perPage, $perPage)->select();

        $itemPerPage = $perPage;
        $pageUrl = $this->baseUrl . '/Admin/Order/detail';
        $paramQuery = array('ad'=>$ad, 'site'=>$site);
        $paginator = new Paginator($currPage, $itemCount, $itemPerPage, $pageUrl, $paramQuery);

        $page = $paginator->getHtml(1);

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->assign('ad', $ad);
        $this->assign('site', $site);

        $this->display();
    }

    public function add()
    {
        $site_code = I('site_code');
        $site_code = 'cf9e5467a9a7b45dd6ac146abcee8ed5';
        $ad_putRelationship = M("putrelationship");

        //广告和网站相关信息
        $putRelationship = $ad_putRelationship->where(array('site_code'=>$site_code))
            ->field('advertising_id, advertising_name, site_id, site_name')
            ->find();

        print_r($putRelationship);
        /*
         * `order_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单主键',
  `advertising_id` int(11) NOT NULL COMMENT '广告ID',
  `advertising_name` varchar(400) NOT NULL COMMENT '广告名称',
  `site_id` int(11) NOT NULL COMMENT '网站ID',
  `site_name` varchar(400) NOT NULL COMMENT '网站名称',
  `site_only` int(11) NOT NULL COMMENT '网站返回用户唯一标示',
  `site_parameter` varchar(200) DEFAULT NULL COMMENT '网站返回用户参数',
  `advertising_code` varchar(50) NOT NULL COMMENT '给广告主生成的唯一标示用户的标志(md5加密(时间戳+advertising_id+site_id))',
  `advertising_only` varchar(200) NOT NULL COMMENT '广告主返回用户唯一标示',
  `advertising_parameter` varchar(200) DEFAULT NULL COMMENT '广告主返回用户参数',
  `finish_step` varchar(50) NOT NULL COMMENT '已经完成步骤(以逗号分隔)',
  `clearing_step` varchar(50) NOT NULL COMMENT '已经结算步骤(以逗号分隔)',
  `back_time` int(11) NOT NULL COMMENT '广告主最后一次返回时间',
  `clearing_time` int(11) NOT NULL COMMENT '最后一次结算时间',
  `finish_gift` int(11) NOT NULL COMMENT '完成奖励：0(约0元)',
  `create_time` int(11) NOT NULL COMMENT '创建时间',
  `update_time` int(11) NOT NULL COMMENT '更新时间',
         */

    }

    /**
     * 网站对接
     */
    public function siteAdd()
    {
        $site_code = I('site');
        $ad_putRelationship = M("putrelationship");

        //广告和网站相关信息
        $putRelationship = $ad_putRelationship->where(array('site_code'=>$site_code))
            ->field('advertising_id, advertising_name, site_id, site_name, advertising_code, site_code')
            ->find();

        /*
         * {"site_only":123,"site_parameter":{"email":"hzhilamp@163.com","username":"lzw"}}
         */

        $data = array(
            'advertising_id'   => $putRelationship['advertising_id'],
            'advertising_name' => $putRelationship['advertising_name'],
            'advertising_code' => $putRelationship['advertising_code'],
            'site_id'          => $putRelationship['site_id'],
            'site_name'        => $putRelationship['site_name'],
            'site_code'        => $putRelationship['site_code'],
            'create_time'      => time()
        );

        if (!empty($site_only)) {
            $data['site_only'] = $site_only;
        }

        if (!empty($site_parameter)) {
            $data['site_parameter'] = $site_parameter;
        }

        $ad_order = M('order');
        $re = $ad_order->add($data);
        print_r($re);


    }

}


?>